package chat.dao;

import java.sql.Connection;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import chat.main.ConnectionManager;
import chat.model.Message;

public class MessageDAO {
	public static int saveMessage(Message s) {
		String query = "insert into message (posted_on, user_name, message) values (?, ?, ?)";
		Connection currentCon = null;
		int result = 0;
		PreparedStatement preStatement = null;
		try {
			currentCon = ConnectionManager.getConnection();
			preStatement = currentCon.prepareStatement(query);
			preStatement.setTimestamp(1, s.getPostedDate());
			preStatement.setString(2, s.getUsername());
			preStatement.setString(3, s.getMessage());
			result = preStatement.executeUpdate();
		} catch (Exception e) {
			e.getStackTrace();
		} finally {
			if (preStatement != null) {
				try {
					preStatement.close();
				} catch (Exception e) {
					preStatement = null;
				}
			}
			if (currentCon != null) {
				try {
					currentCon.close();
				} catch (Exception e) {
					currentCon = null;
				}
			}
		}

		return result;
	}

	public static List<Message> getAllMessage() {
		Connection currentCon = null;
		Statement statement = null;
		ResultSet rs = null;
		ArrayList<Message> listMessage = new ArrayList<>();
		String query = "select * from message";
		try {
			currentCon = ConnectionManager.getConnection();
			statement = currentCon.createStatement();
			rs = statement.executeQuery(query);
			while (rs.next()) {
				Message msg = new Message();
				msg.setId(rs.getInt(1));
				msg.setPostedDate(rs.getTimestamp(2));
				msg.setUsername(rs.getString(3));
				msg.setMessage(rs.getString(4));
				listMessage.add(msg);
			}

		} catch (Exception e) {
			e.getStackTrace();
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (Exception e) {
					statement = null;
				}
			}
			if (currentCon != null) {
				try {
					currentCon.close();
				} catch (Exception e) {
					currentCon = null;
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
					rs = null;
				}
			}
		}
		return listMessage;
	}

	public static List<Message> getNewMessage(Timestamp time) {
		Connection currentCon = null;
		Statement statement = null;
		ResultSet rs = null;
		ArrayList<Message> listMessage = new ArrayList<>();
		String query = "select * from message where posted_on > '" + time + "'";
		try {
			currentCon = ConnectionManager.getConnection();
			statement = currentCon.createStatement();
			rs = statement.executeQuery(query);
			while (rs.next()) {
				Message msg = new Message();
				msg.setId(rs.getInt(1));
				msg.setPostedDate(rs.getTimestamp(2));
				msg.setUsername(rs.getString(3));
				msg.setMessage(rs.getString(4));
				listMessage.add(msg);
			}

		} catch (Exception e) {
			e.getStackTrace();
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (Exception e) {
					statement = null;
				}
			}
			if (currentCon != null) {
				try {
					currentCon.close();
				} catch (Exception e) {
					currentCon = null;
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e) {
					rs = null;
				}
			}
		}
		return listMessage;
	}
}
